//
//  LOHomeThreeTableViewCell.m
//  renmingongshe
//
//  Created by macbook on 2017/10/20.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "LOHomeThreeTableViewCell.h"

@implementation LOHomeThreeTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (void)showData:(NSDictionary *)data
{
    [self.addView removeAllSubviews];
    if ([data[@"catName"]isEqualToString:@""]) {
        
    }
    NSArray *goods = data[@"goods"];
    _typeName.text = [NSString stringWithFormat:@"%@ >", data[@"catName"]];

    [_typeIma sd_setImageWithURL:[NSURL URLWithString:[Utils getImaSting:data[@"img"]]]];
    if (goods.count == 4) {
        [self creatFourView:goods[0]];
        
         UIView *v2 = [self creatFourView:goods[1]];
        [v2 setLeft:WIDTH/2];
        
         UIView *v3 = [self creatFourView:goods[2]];
        [v3 setTop:v2.bottom+1];
        
         UIView *v4 = [self creatFourView:goods[3]];
        [v4 setTop:v2.bottom+1];
        [v4 setLeft:WIDTH/2];
    }else if(goods.count == 6){
        [self creatSixView:goods[0]];
        
        UIView *v2 = [self creatSixView:goods[1]];
        [v2 setLeft:WIDTH/3];
        
        UIView *v3 = [self creatSixView:goods[2]];
        [v3 setLeft:WIDTH*2/3];
        
        UIView *v4 = [self creatSixView:goods[3]];
        [v4 setTop:v2.bottom+1];
        
        UIView *v5 = [self creatSixView:goods[4]];
        [v5 setTop:v2.bottom+1];
        [v5 setLeft:WIDTH/3];
        
        UIView *v6 = [self creatSixView:goods[5]];
        [v6 setTop:v2.bottom+1];
        [v6 setLeft:WIDTH*2/3];
    }else if(goods.count == 3){
        [self creatFourView:goods[0]];
        
        UIView *v2 = [self creatFourView:goods[1]];
        [v2 setTop:91];
        
        UIView *v3 = [self creatThreeView:goods[2]];
        [v3 setLeft:v2.right+1];
    }
}

- (void)btnclick:(UIButton *)btn
{
    self.btnclick(btn.tag);
}

//三个小商品的右边展示图
- (UIView *)creatThreeView:(NSDictionary *)dic
{
    UIView *vi = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/2, 180)];
    
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, WIDTH/2-20, 20)];
    name.font = [UIFont systemFontOfSize:15.0f];
    name.textColor = [UIColor colorWithHex:0x333333];
    name.text = [dic objectForKey:@"goodsName"];
    [vi addSubview:name];
    
    UILabel *price = [[UILabel alloc]initWithFrame:CGRectMake(10, name.bottom+5, 100, 20)];
    price.textColor = [UIColor colorWithRed:139/255.0 green:139/255.0 blue:139/255.0 alpha:1.0];
    price.font = [UIFont systemFontOfSize:14.0f];
    price.text = [NSString stringWithFormat:@"￥%.2f", [[dic objectForKey:@"shopPrice"] floatValue]];
    [vi addSubview:price];
    
    UIImageView *ima = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH/2-10-70, 30, 70, vi.height-30)];
    [ima sd_setImageWithURL:[NSURL URLWithString:[Utils getImaSting:[dic objectForKey:@"goodsImg"]]]];
    [vi addSubview:ima];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/2, 1)];
    line.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [vi addSubview:line];
    
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, vi.height)];
    line2.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [vi addSubview:line2];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:vi.frame];
    btn.tag = [[dic objectForKey:@"goodsId"] integerValue];
    [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    [vi addSubview:btn];
    
    [self.addView addSubview:vi];
    return vi;
}

- (UIView *)creatFourView:(NSDictionary *)dic
{
    UIView *vi = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/2, 90)];
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, WIDTH/2-20, 20)];
    name.font = [UIFont systemFontOfSize:15.0f];
    name.textColor = [UIColor colorWithHex:0x333333];
    name.text = [dic objectForKey:@"goodsName"];
    [vi addSubview:name];
    
    UILabel *price = [[UILabel alloc]initWithFrame:CGRectMake(10, name.bottom+5, 100, 20)];
    price.textColor = [UIColor colorWithRed:139/255.0 green:139/255.0 blue:139/255.0 alpha:1.0];
    price.font = [UIFont systemFontOfSize:14.0f];
    price.text = [NSString stringWithFormat:@"￥%.2f", [[dic objectForKey:@"shopPrice"] floatValue]];
    [vi addSubview:price];
    
    UIImageView *ima = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH/2-10-70, 30, 70, vi.height-31)];
    [ima sd_setImageWithURL:[NSURL URLWithString:[Utils getImaSting:[dic objectForKey:@"goodsImg"]]]];
    [vi addSubview:ima];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/2, 1)];
    line.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [vi addSubview:line];
    
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, vi.height)];
    line2.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [vi addSubview:line2];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:vi.frame];
    [vi addSubview:btn];
    btn.tag = [[dic objectForKey:@"goodsId"] integerValue];
    [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.addView addSubview:vi];
    return vi;
}

- (UIView *)creatSixView:(NSDictionary *)dic
{
    UIView *vi = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/3, 110)];
    
    UIImageView *ima = [[UIImageView alloc]initWithFrame:CGRectMake(20, 5, WIDTH/3-40, 60)];
    [ima sd_setImageWithURL:[NSURL URLWithString:[Utils getImaSting:[dic objectForKey:@"goodsImg"]]]];
    [vi addSubview:ima];
    
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(0, ima.bottom+5, WIDTH/3, 20)];
    name.font = [UIFont systemFontOfSize:15.0f];
    name.textAlignment = NSTextAlignmentCenter;
    name.textColor = [UIColor colorWithHex:0x333333];
    name.text = [dic objectForKey:@"goodsName"];
    [vi addSubview:name];
    
    UILabel *price = [[UILabel alloc]initWithFrame:CGRectMake(0, name.bottom, WIDTH/3, 20)];
    price.textColor = [UIColor colorWithRed:139/255.0 green:139/255.0 blue:139/255.0 alpha:1.0];
    price.font = [UIFont systemFontOfSize:14.0f];
    price.textAlignment = NSTextAlignmentCenter;
    price.text = [NSString stringWithFormat:@"￥%.2f", [[dic objectForKey:@"shopPrice"] floatValue]];
    [vi addSubview:price];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/2, 1)];
    line.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [vi addSubview:line];
    
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, vi.height)];
    line2.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [vi addSubview:line2];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:vi.frame];
    [vi addSubview:btn];
    btn.tag = [[dic objectForKey:@"goodsId"] integerValue];
    [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.addView addSubview:vi];
    return vi;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
